package com.onlylemi.mapview.library.layer;


import com.onlylemi.mapview.library.MapView;
import com.onlylemi.mapview.library.ResourceTable;
import com.onlylemi.mapview.library.utils.MapUtils;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.Point;
import ohos.media.image.PixelMap;
import ohos.multimodalinput.event.TouchEvent;

import java.security.SecureRandom;
import java.util.List;

/**
 * RouteLayer
 *
 * @author: onlylemi
 */
public class RouteLayer extends MapBaseLayer {

    private List<Integer> routeList; // routes list
    private List<Point> nodeList; // nodes list

    private float routeWidth; // the width of route

    private PixelMap routeStartBmp;
    private PixelMap routeEndBmp;

    private Paint paint;

    public RouteLayer(MapView mapView) {
        this(mapView, null, null);
    }

    public RouteLayer(MapView mapView, List<Point> nodeList, List<Integer> routeList) {
        super(mapView);
        this.nodeList = nodeList;
        this.routeList = routeList;

        initLayer();
    }

    private void initLayer() {
        this.routeWidth = 10;

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);

        routeStartBmp = MapUtils.getPixelMap(mapView.getContext(), ResourceTable.Media_start_point, 76, 104);
        routeEndBmp = MapUtils.getPixelMap(mapView.getContext(), ResourceTable.Media_end_point, 76, 104);
    }

    @Override
    public void onTouch(TouchEvent event) {

    }

    @Override
    public void draw(Canvas canvas, Matrix currentMatrix, float currentZoom, float
            currentRotateDegrees) {
        if (isVisible && routeList != null && nodeList != null) {
            canvas.save();

            drawing:
            if (!routeList.isEmpty() && !nodeList.isEmpty()) {
                // draw route
                for (int i = 0; i < routeList.size() - 1; i++) {

                    if (routeList.get(i) >= nodeList.size() || routeList.get(i + 1) >= nodeList.size()) {
                        break drawing;
                    }

                    float[] goal1 = {nodeList.get(routeList.get(i)).getPointX(),
                            nodeList.get(routeList.get(i)).getPointY()};
                    float[] goal2 = {nodeList.get(routeList.get(i + 1)).getPointX(),
                            nodeList.get(routeList.get(i + 1)).getPointY()};
                    currentMatrix.mapPoints(goal1);
                    currentMatrix.mapPoints(goal2);

                    //For test
                    paint.setColor(new Color(Color.rgb(new SecureRandom().nextInt(255),
                            new SecureRandom().nextInt(255), new SecureRandom().nextInt(255))));
                    paint.setStrokeWidth(routeWidth);
                    canvas.drawLine(goal1[0], goal1[1], goal2[0], goal2[1], paint);
                }

                // draw bmp
                float[] goal1 = {nodeList.get(routeList.get(0)).getPointX(),
                        nodeList.get(routeList.get(0)).getPointY()};
                float[] goal2 = {
                        nodeList.get(routeList.get(routeList.size() - 1)).getPointX(),
                        nodeList.get(routeList.get(routeList.size() - 1)).getPointY()};
                currentMatrix.mapPoints(goal1);
                currentMatrix.mapPoints(goal2);
                canvas.drawPixelMapHolder(new PixelMapHolder(routeStartBmp),
                        goal1[0] - (float) routeStartBmp.getImageInfo().size.width / 2, goal1[1]
                                - routeStartBmp.getImageInfo().size.height, paint);
                canvas.drawPixelMapHolder(new PixelMapHolder(routeEndBmp),
                        goal2[0] - (float) routeEndBmp.getImageInfo().size.width / 2, goal2[1]
                                - routeEndBmp.getImageInfo().size.height, paint);
            }

            canvas.restore();
        }
    }

    public void setNodeList(List<Point> nodeList) {
        this.nodeList = nodeList;
    }

    public void setRouteList(List<Integer> routeList) {
        this.routeList = routeList;
    }
}
